#encoding:utf8

import os
import zipfile

def zip_dir(zippath, tarfile):
    filelist = []
    if os.path.isfile(zippath):
        filelist.append(zippath)
    else:
        for dirpath, dirnames, filenames in os.walk(zippath):
            for filename in filenames:
                filelist.append(os.path.join(dirpath, filename))
    f = zipfile.ZipFile(tarfile, 'w', zipfile.ZIP_DEFLATED)
    for tar in filelist:
        arcname = tar[len(zippath):]
        f.write(tar, arcname)
    f.close()